const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
const upScoreNum = 5
const d = new Date()
// 创建集合云函数入口函数
exports.main = async (event, context) => {
  try {
    // 
    let meassage = '增加积分成功了！'
    const students = await db.collection('student')
    const studyLogs = await db.collection('studyLogs')
    if (event.ids != 0) {
      event.ids.forEach(id => {
        students.doc(id).update({
          data: {
            score: _.inc(upScoreNum)
          }
        }).then(res => {
          if (res.stats.updated == 1) {
            studyLogs.add({
              data: {
                student_id: id,
                create_time: d.getTime(),
                score: upScoreNum,
                remark: ''
              }
            })
          } else {
            return {
              success: false,
              data: e
            }
          }
        })
      });
    } else {

      //group: this.data.group,
      //winner: winner
      const group = event.group
      const winners = event.winner
      if (group == 'gender') {
        for (const winner of winners) {
          let student = await students.where({ gender: winner }).field({ _id: true }).get()
          student = student.data
          for (let index = 0; index < student.length; index++) {
            let id = student[index]['_id']
            students.doc(id).update({
              data: {
                score: _.inc(upScoreNum)
              }
            }).then(res => {
              if (res.stats.updated == 1) {
                studyLogs.add({
                  data: {
                    student_id: id,
                    create_time: d.getTime(),
                    score: upScoreNum,
                    remark: ''
                  }
                })
              } else {
                return {
                  success: false,
                  data: e
                }
              }
            })
          }
        }
        if (winners.length == 2) {
          meassage = '男生女生赢了！'
        } else {
          meassage = winners[0] + '生赢了！'
        }
      } else {//group=='group'
        for (const winner of winners) {
          let student = await students.where({ group: winner }).field({ _id: true }).get()
          student = student.data
          for (let index = 0; index < student.length; index++) {
            let id = student[index]['_id']
            students.doc(id).update({
              data: {
                score: _.inc(upScoreNum)
              }
            }).then(res => {
              if (res.stats.updated == 1) {
                studyLogs.add({
                  data: {
                    student_id: id,
                    create_time: d.getTime(),
                    score: upScoreNum,
                    remark: ''
                  }
                })
              } else {
                return {
                  success: false,
                  data: e
                }
              }
            })
          }
        }
        if (winners.length == 2) {
          meassage = '第' + winners[0]+winners[1] + '组赢了！'
        } else {
          meassage = '第' + winners[0] + '组赢了！'
        }
      }
    }

    return {
      success: true,
      data: meassage
    }
  } catch (e) {
    console.log(e)
    return {
      success: false,
      data: e
    }
  }
}